package com.cebbank.dss.springbootredis;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author name:dss
 * @version 1.0
 * @description: TODO
 * @date 2022/9/152022/9/15
 */
public class redistest {
    public static void main(String[] args) {

    }
    @Test
    public void jedisTest(){

        Jedis jedis = null;
        try {
        //连接redis
        jedis = new Jedis("192.168.18.130",6379);
        //密码执行认证方法
        jedis.auth("redis@redis");
        //字符串String操作
        jedis.set("name","xiaoming");
        String value=jedis.get("name");
        System.out.println("name:"+value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis!=null){
                //关闭连接
                jedis.close();
            }
        }
    }


    @Test
    public void redisPool(){
        //创建配置对象
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //最大允许连接数
        jedisPoolConfig.setMaxTotal(10);
        //最大空闲连接
        jedisPoolConfig.setMaxIdle(10);

        //初始化jedis连接池
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.18.130", 6379);
        Jedis jedis = null;
        try{
            //从连接池获取jedis对象
            jedis = jedisPool.getResource();
            jedis.auth("redis@redis");
            //执行操作
            jedis.set("age", "11");
            String value = jedis.get("age");
            System.out.println("age:" + value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis!=null) {
                //关闭连接
                jedis.close();
            }
        }
    }
}
